﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Twilight.Controls;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Silverlight.Testing;

namespace Twilight.Tests
{
    [TestClass]
    public class TextWrapPanelFixture : SilverlightTest
    {
        [TestMethod]
        public void AddChildrenTest()
        {
            string testText = "this is a test to see if bryant coded this properly";

            TestWrapPanel panel = new TestWrapPanel();
            panel.SetText(testText);

            string[] words = testText.Split(" ".ToCharArray());

            Assert.AreEqual<int>(words.Length - 1, panel.Children.Count); // should be one less word than counted due to regex
        }

        [TestMethod]
        public void WrapTextBlocksSixTimes()
        {
            string testText = "This text should wrap six times.";

            TextBlock sample = new TextBlock() { Text = "should " }; // need space on end since we add a space
            this.TestPanel.Children.Add(sample);
            sample.Measure(new Size(200, 400));
            var sampleSize = sample.DesiredSize;
            this.TestPanel.Children.Clear();


            TestWrapPanel panel = new TestWrapPanel();
            this.TestPanel.Children.Add(panel);
            panel.SetText(testText);
            panel.Measure(new Size(sampleSize.Width, 400));

            Assert.AreEqual<double>(sampleSize.Width, panel.DesiredSize.Width); // width should be largest element which was word "Should";
            Assert.AreEqual<double>(sampleSize.Height * 6.0, panel.DesiredSize.Height); // should wrap six times
        }

        [TestMethod]
        public void WrapTextBlocksTwice()
        {
            string testText = "This text should wrap twice.";

            TextBlock sample = new TextBlock() { Text = "This text should " }; // need space on end since we add a space
            this.TestPanel.Children.Add(sample);
            sample.Measure(new Size(200, 400));
            var sampleSize = sample.DesiredSize;
            this.TestPanel.Children.Clear();


            TestWrapPanel panel = new TestWrapPanel();
            this.TestPanel.Children.Add(panel);
            panel.SetText(testText);
            panel.Measure(new Size(sampleSize.Width, 400));

            Assert.AreEqual<double>(sampleSize.Width, panel.DesiredSize.Width); // width should be largest element which was word "Should";
            Assert.AreEqual<double>(sampleSize.Height * 2.0, panel.DesiredSize.Height); // should wrap 2 times
        }

        public class TestWrapPanel : TextWrapPanel
        {
            public TestWrapPanel()
            {
                linkRegex = new Regex("bryant", RegexOptions.Singleline | RegexOptions.ExplicitCapture);
            }

            protected override void ProcessLinkMatch(System.Text.RegularExpressions.Match match)
            {
                // do nothing
            }

            public void SetText(string text)
            {
                OnTextChanged(text);
            }
        }
    }
}
